// Definition for a binary tree node.
function TreeNode(val, left, right) {
    this.val = (val===undefined ? 0 : val)
    this.left = (left===undefined ? null : left)
    this.right = (right===undefined ? null : right)
}
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isValidBST = function(root) {
  const longMin = -(2 ** 31) - 1, longMax = 2 ** 31;
  const isValidScope = (root, minValue, maxValue) => {
    if(root == null)
      return true;
    if(root.val >= maxValue || minValue >= root.val) {
      return false
    }
    return isValidScope(root.left, minValue, root.val) && isValidScope(root.right, root.val, maxValue);
  }
  return isValidScope(root, longMin, longMax);
};
